#!/bin/bash
MODULE=$1
ENV=$2
WORKDIR=$4
LINUXUSER=$4
PORT=1
JDK=OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz

case "$MODULE" in
  "service-gateway")
    PORT=8333
    ;;
  "service-sequence")
    PORT=8308
    ;;
  "service-live")
    PORT=8306
     ;;
  "service-wechat")
   PORT=8305
    ;;
  "service-user")
   PORT=8304
    ;;
  "service-activity")
   PORT=8303
    ;;
  "service-order")
   PORT=8302
    ;;
  "service-vod")
   PORT=8301
    ;;
  *)
    echo "Unknown"
    ;;
esac

# 启动日志
DIR=~/projects/$MODULE/
LOG_PATH=$DIR/log
RESTART_LOG=$LOG_PATH/restart-$MODULE.log
HIS_PATH=$DIR/history
if [ ! -d "$LOG_PATH" ]; then
    mkdir -p "$LOG_PATH"
fi

if [ ! -d "$HIS_PATH" ]; then
    mkdir -p "$HIS_PATH"
fi

echo '================================' >> $RESTART_LOG
echo "using docker: "`date +%Y%m%d' '%H:%M:%S` >> $RESTART_LOG
echo '================================' >> $RESTART_LOG
echo "PORT: "$PORT  >> $RESTART_LOG
echo "MODULE: "$MODULE  >> $RESTART_LOG
echo "ENV: "$ENV  >> $RESTART_LOG
cd $DIR
echo 'PWD: '$(pwd) >> $RESTART_LOG

VERSION=$(date +%Y%m%d%H%M%S)
MV=${MODULE}-${VERSION}
echo "MV: "$MV  >> $RESTART_LOG

docker stop $(docker ps -a --filter "name=${MODULE}" --format "{{.ID}}")
docker rm $(docker ps -a --filter "name=${MODULE}" --format "{{.ID}}")
docker rmi $(docker images|grep ${MODULE} | cut -f 1 -d ' ')
echo "==> 1. stop and rm container"  >> $RESTART_LOG

sed -i "s/@PORT/$PORT/g" Dockerfile-1.0
sed -i "s/@MODULE/$MODULE/g" Dockerfile-1.0
sed -i "s/@WORKDIR/$WORKDIR/g" Dockerfile-1.0
echo "==> 2. edit Dockerfile-1.0"  >> $RESTART_LOG

sed -i "s/@MODULE/$MODULE/g" runInDock.sh
sed -i "s/@MV/$MV/g" runInDock.sh
sed -i "s/@ENV/$ENV/g" runInDock.sh
echo "==> 2. edit runInDock.sh"  >> $RESTART_LOG

cp /home/$LINUXUSER/install/$JDK $DIR/release

# 清理端口进程
PID=$(jps | grep $MODULE | cut -f 1 -d ' ')
echo "jps  PID: "$PID >> $RESTART_LOG
if [[ $PID =~ ^[0-9]+$ ]]
then
	echo 'kill -9 '$PID >> $RESTART_LOG
	kill -9 $PID
fi

PID=$(lsof -i :$PORT | head -n 2 | grep java | awk '{printf $2}')
echo "lsof PID: "$PID >> $RESTART_LOG
if [[ $PID =~ ^[0-9]+$ ]]
then
	echo 'kill -9 '$PID >> $RESTART_LOG
	kill -9 $PID
fi

PID=$(netstat -tpln|grep $PORT | cut -d 'N' -f2 | sed -s "s/\/java//g" | sed -s 's/ //g')
echo "netstat PID: "$PID >> $RESTART_LOG
if [[ $PID =~ ^[0-9]+$ ]]
then
	echo 'kill -9 '$PID >> $RESTART_LOG
	kill -9 $PID
fi

docker build -f Dockerfile-1.0 -t $MV .
echo "==> 3. build Dockerfile completed"  >> $RESTART_LOG

docker run -itd -v /home/$LINUXUSER/projects/$MODULE/log:/$WORKDIR/log:z --privileged=true --name $MV -p $PORT:$PORT $MV
echo "==> 4. docker run"  >> $RESTART_LOG

if [ "$3" = "2" -o "$3" = "3" ]
then
        tail -fn 200 $LOG_PATH/$MODULE-nohup.log
fi

# 备份本次的发布到历史文件夹
echo '================ mv 1================' >> $RESTART_LOG
# 重命名发布文件并且移动到 ~/projects 文件夹，等待发布
NEW_NAME=${MV}.jar
mv $DIR/release/$MODULE.jar $DIR/release/$NEW_NAME
mv $DIR/release/$NEW_NAME $DIR/history
echo '================ mv 2================' >> $RESTART_LOG

# 切换到目标文件夹
HIS_PATH=$DIR/history
echo 'HIS_PATH: '$HIS_PATH >> $RESTART_LOG
cd $HIS_PATH

rm $DIR/release/$JDK
# 列出目录中的文件，按修改时间排序，并跳过最新的两个文件
files_to_delete=$(ls -t | tail -n +3)

# 循环删除文件
for file in $files_to_delete; do
    rm -f "$file"
    echo "rm old jar: $file" >> $RESTART_LOG
done


